package com.ptswitch.material.util.security;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.security.Permission;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.model.security.RolePermission;
import com.ptswitch.material.model.security.UserRole;

public class RoleEditUtil {

    public static List<RolePermission> toNewRolePermissions(List<Role> nows, List<Role> origs,//
            Permission permission) {
        List<RolePermission> result = new ArrayList<RolePermission>();
        for (Role now : nows) {
            if (origs.indexOf(now) != -1) {
                RolePermission rp = new RolePermission(now, permission);
                result.add(rp);
            }
        }
        return result;
    }

    public static List<RolePermission> toNewRolePermissions(List<Permission> nows, List<Permission> origs,//
            Role role) {
        List<RolePermission> result = new ArrayList<RolePermission>();
        for (Permission now : nows) {
            if (origs.indexOf(now) != -1) {
                RolePermission rp = new RolePermission(role, now);
                result.add(rp);
            }
        }
        return result;
    }

    public static List<UserRole> toNewUserRoles(List<Role> nows, List<Role> origs,//
            Uzer user) {
        List<UserRole> result = new ArrayList<UserRole>();
        for (Role now : nows) {
            if (origs.indexOf(now) != -1) {
                UserRole ur = new UserRole(user, now);
                result.add(ur);
            }
        }
        return result;
    }

    public static List<UserRole> toNewUserRoles(List<Uzer> nows, List<Uzer> origs,//
            Role role) {
        List<UserRole> result = new ArrayList<UserRole>();
        for (Uzer now : nows) {
            if (origs.indexOf(now) != -1) {
                UserRole ur = new UserRole(now, role);
                result.add(ur);
            }
        }
        return result;
    }

    public static List<Role> getRoles(Permission permission) {
        List<Role> result = new ArrayList<Role>();
        for (RolePermission rp : permission.getRolePermissions()) {
            result.add(rp.getRole());
        }
        return result;
    }

    public static List<Role> getRoles(Uzer user) {
        List<Role> result = new ArrayList<Role>();
        if (user.getUserRoles() == null) {
            return Collections.emptyList();
        }
        for (UserRole ur : user.getUserRoles()) {
            result.add(ur.getRole());
        }
        return result;
    }

    public static List<Permission> getPermissions(Role role) {
        List<Permission> result = new ArrayList<Permission>();
        for (RolePermission rp : role.getRolePermissions()) {
            result.add(rp.getPermission());
        }
        return result;
    }

    public static List<Uzer> getUsers(Role role) {
        List<Uzer> result = new ArrayList<Uzer>();
        for (UserRole ur : role.getUserRoles()) {
            result.add(ur.getUser());
        }
        return result;
    }
}
